using System;
using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class PlayerTraining
{
	public delegate void OnSave();

	public delegate void OnTrainingFinished();

	private OnSave onSave;

	private PlayerModel model;

	private PlayerStats playerStats;

	public event OnTrainingFinished onTrainingFinished;

	public PlayerTraining(PlayerModel model, OnSave onSave, PlayerStats playerStats)
	{
		this.model = model;
		this.onSave = onSave;
		this.playerStats = playerStats;
	}

	public void ReloadModel(PlayerModel model)
	{
		this.model = model;
	}

	public bool isTrainingFinished()
	{
		return DateTime.Now >= new DateTime(model.timeToTrainingFinished);
	}

	public TimeSpan TimeTillTrainingFinished()
	{
		if (isTrainingFinished())
		{
			return new TimeSpan(0L);
		}
		return new DateTime(model.timeToTrainingFinished).Subtract(DateTime.Now);
	}

	public void StartTraining(TrainingConfig.Training training)
	{
		WalletManager walletManager = GGPlayerSettings.instance.walletManager;
		if (training.currencyType == CurrencyType.coins)
		{
			walletManager.BuyItemCoins(training.price);
		}
		else if (training.currencyType == CurrencyType.ggdollars)
		{
			walletManager.BuyItemDollars(training.price);
		}
		BehaviourSingleton<EnergyManager>.instance.SpendEnergy(training.energyCost);
		model.timeToTrainingFinished = DateTime.Now.Add(GetTrainingDurationForPlayer(training.GetTrainingDuration())).Ticks;
		SaveNewTraining(training);
		AddTrainingToScheduler();
		if (training.price > 0)
		{
			Analytics.instance.MoneySpentOnTraining(training);
		}
	}

	private void SaveNewTraining(TrainingConfig.Training training)
	{
		if (model.activeTraining == null)
		{
			model.activeTraining = new List<Training>();
		}
		foreach (TrainingConfig.StatUpdateInfo item in training.statUpdateInfo)
		{
			Training training2 = new Training();
			training2.trainingType = item.trainingType;
			training2.statIndex = ((training2.trainingType != 0) ? ((int)item.skillType) : ((int)item.attrType));
			training2.statVal = item.totalBoost;
			model.activeTraining.Add(training2);
		}
		DoOnSave();
	}

	private void DoOnSave()
	{
		if (onSave != null)
		{
			onSave();
		}
	}

	public void ApplyTrainingIfFinished()
	{
		if (!isTrainingFinished() || model.activeTraining.Count == 0)
		{
			return;
		}
		GGPlayerSettings instance = GGPlayerSettings.instance;
		LevelUpLayer.LevelProgressInfo levelProgressInfo = default(LevelUpLayer.LevelProgressInfo);
		levelProgressInfo.levelProgress = instance.LevelProgress();
		levelProgressInfo.levelName = instance.LevelName();
		LevelUpLayer.LevelProgressInfo levelProgressInfo2 = levelProgressInfo;
		bool flag = false;
		bool flag2 = false;
		foreach (Training item in model.activeTraining)
		{
			if (item.trainingType == TrainingType.Attribute)
			{
				playerStats.UpdateAttribute(item.statVal, item.statIndex);
				flag = true;
			}
			else if (item.trainingType == TrainingType.Skill)
			{
				playerStats.UpdateSkill(item.statVal, item.statIndex);
				flag2 = true;
			}
		}
		model.activeTraining.Clear();
		DoOnSave();
		if (this.onTrainingFinished != null)
		{
			this.onTrainingFinished();
		}
	}

	public void AddTrainingToScheduler()
	{
		BehaviourSingleton<GGScheduler>.instance.AddListener(ApplyTrainingIfFinished, new DateTime(model.timeToTrainingFinished));
	}

	public TimeSpan GetTrainingDurationForPlayer(TimeSpan baseDuration)
	{
		long ticks = new TimeSpan(0, 0, 0).Ticks;
		long ticks2 = baseDuration.Ticks;
		long ticks3 = (long)Mathf.Max(ticks, ticks2);
		return new TimeSpan(ticks3);
	}
}
